package main

import (
	"flag"
	"fmt"
	"net/http"
	handlerUp "sjy/auto_bd/controller"

	"github.com/gin-gonic/gin"
)

func main() {
	var confFile string
	flag.StringVar(&confFile, "c", "", "config file")
	flag.Parse()

	r := gin.Default()

	// 错误处理
	r.Use(recoverMiddleware())

	r.GET("/up", func(ctx *gin.Context) {

		// 设置响应头，确保客户端正确解析流式传输的数据
		ctx.Header("Content-Type", "text/plain")
		ctx.Header("Transfer-Encoding", "chunked")

		// 逻辑入口
		handlerUp.Execute(confFile, ctx.Writer)
		ctx.String(http.StatusOK, "ok!")
	})
	r.Run(":11000")
}

func recoverMiddleware() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				fmt.Println(err)
				ctx.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
					"error": fmt.Sprintf("%v", err),
				})
			}

		}()
		ctx.Next()
	}
}
